***** Replication for (TITLE) ******

*controls style of the figures and stops from having to click to continue to do file
set more off
set scheme s1mono

*********EXPERIMENT 1*********

*Load the data (directory needs to be set so Stata knows where it is)
use "Experiment1.dta", clear

**Generate Outcome Scales for Experiment 1 and examine reliability
*Social Distancing Scale 
gen sd_scale = (maskwearing_likely+agree_liketosocdist+rc_likely_electivemed+rc_likely_friendshouse+rc_likely_familyhouse+rc_likely_smallparty)/6
alpha maskwearing_likely agree_liketosocdist rc_likely_electivemed rc_likely_friendshouse rc_likely_familyhouse rc_likely_smallparty
*Food Scale
gen food_scale = (rc_likely_outtoeat + rc_likely_coffeeshop + restaurant_binary_in_vs_all)/3
alpha rc_likely_outtoeat rc_likely_coffeeshop restaurant_binary_in_vs_all
*Others Scale
gen others_scale = (persuade_likely + report_likely + noncompliers_selfish + rc_noncompliers_trustworthy + rc_noncompliers_likeable + rc_noncompliers_competent)/6
alpha persuade_likely  report_likely  noncompliers_selfish  rc_noncompliers_trustworthy  rc_noncompliers_likeable  rc_noncompliers_competent
*Norms/Beliefs Scale
gen normsbeliefs_scale = (agree_socdiststopmesick+agree_socdiststopotherssick+agree_guiltyifnotsocdist)/3
alpha agree_socdiststopmesick agree_socdiststopotherssick agree_guiltyifnotsocdist

*** Core Analysis for Experiment 1 (appears in main text and tables in S2 Appendix)***
*This function estimates models for all of the core outcomes and store the results
foreach var of varlist normsbeliefs_scale sd_scale food_scale others_scale {
	regress `var' treat_* lucid_age lucid_female lucid_hhi lucid_incmissing i.lucid_education partisanship_* i.lucid_region, robust
	est store `var'
	qui summ `var' if e(sample)
}

*Plot Coefficients for Figure 1
*Panel A Beliefs
coefplot normsbeliefs_scale, keep(*:treat*) omitted baselevels xline(0) yline(6.5 8.5) title("Effects by Message and Outcome", size(small)) xlabel(, labels labsize(vsmall)) ylabel(, labels labsize(small))  title("A: BELIEFS") name(F1a, replace) fxsize(60) 
*Panel C Food
coefplot food_scale, keep(*:treat*) omitted baselevels xline(0) yline(6.5 8.5) title("Effects by Message and Outcome", size(small)) xlabel(, labels labsize(vsmall)) ylabel(, labels labsize(small)) title("C: FOOD") name(F1c, replace) fxsize(60)

*Replace var labels with a black space to supress axis labels
preserve
foreach var of varlist treat_notbrave treat_notbravewpollution treat_sVo_Is_As treat_sVo_Is_Atc treat_sVo_Is_Alc treat_sVo_Io_As treat_sVo_Io_Atc treat_sVo_Io_Alc treat_returnnormal treat_newnormal {
	label var `var' " "
}

*Panel B Social Distancing Scale
coefplot sd_scale, keep(*:treat*) omitted baselevels xline(0) yline(6.5 8.5) title("Effects by Message and Outcome", size(small)) xlabel(, labels labsize(vsmall)) ylabel(, labels labsize(small)) title("B: DISTANCING") name(F1b, replace) fxsize(40)
*Panel D Others 
coefplot others_scale, keep(*:treat*) omitted baselevels xline(0) yline(6.5 8.5) title("Effects by Message and Outcome", size(small)) xlabel(, labels labsize(vsmall)) ylabel(, labels labsize(small))  title("D: OTHERS") name(F1d, replace) fxsize(40)
restore

*Combine the graphs that were just generated to make Figure 1
graph combine F1a F1b F1c F1d, rows(2) title("") note("", size(vsmall)) xsize(12) ysize(8.5) scale(.7)
graph export graphs\Figure1.pdf, replace

****Creates Figures for the Appendicies

*Figure for Distribution of Outcomes as appears in S2 Appendix 
foreach var of varlist sd_scale food_scale others_scale normsbeliefs_scale {
	summ `var'
	local t1=strofreal(r(mean),"%9.2f")
	local t2=strofreal(r(sd),"%9.2f")
	twoway (histogram `var', discrete fraction), name(`var', replace) note("Mean = `t1'; SD=`t2'")
	}

*graph combine normsbeliefs_scale sd_scale food_scale others_scale absentee_likely  gotopolls_likely vaccinetakeup, title("Figure A1: Experiment 1, Outcomes") xsize(8.5) ysize(11) scale(.8)
graph combine normsbeliefs_scale sd_scale food_scale others_scale, xsize(8.5) ysize(11) scale(.8)
graph export graphs\FigureA1DistOfOutcomes.pdf, replace


*Heterogeneous treatment effects

*Generate liberty scale for HTE
gen liberty_scale = (liberty1+ liberty2+liberty3+abs(liberty4-1)+liberty5)/5
label var liberty_scale "Liberty endorsement (Scale 0-1)"


*Create subgroups and run models for HTE
forvalues ctr = 1(1)11 {
	
	preserve

	if `ctr'==1 {
		local subgroup = "Eliminate very fast/slow respondents"
		local covariates = "lucid_age lucid_female lucid_hhi lucid_incmissing i.lucid_education partisanship_* i.lucid_region"
		local sn = "01_speed"
		drop if duration<60*4 | duration>60*20
		}
	if `ctr'==2 {
		local subgroup "Young (Age<35)"
		local covariates = "lucid_female lucid_hhi lucid_incmissing i.lucid_education partisanship_* i.lucid_region"
		local sn = "02_young"
		drop if lucid_age>=35
		}
	if `ctr'==3 {
		local subgroup "Old (Age>=35)"
		local covariates = "lucid_female lucid_hhi lucid_incmissing i.lucid_education partisanship_* i.lucid_region"
		local sn = "03_old"
		drop if lucid_age<35
		}
	if `ctr'==4 {
		local subgroup "Men"
		local covariates = "lucid_age lucid_hhi lucid_incmissing i.lucid_education partisanship_* i.lucid_region"
		local sn = "04_men"
		drop if lucid_female~=0
		}
	if `ctr'==5 {
		local subgroup "Women"
		local covariates = "lucid_age lucid_hhi lucid_incmissing i.lucid_education partisanship_* i.lucid_region"
		local sn = "05_women"
		drop if lucid_female~=1
		}
	if `ctr'==6 {
		local subgroup "Northeast"
		local covariates = "lucid_age lucid_female lucid_hhi lucid_incmissing i.lucid_education partisanship_*"
		local sn = "06_northeast"
		drop if lucid_region~=3
		}
 	if `ctr'==7 {
		local subgroup "NOT Northeast"
		local covariates = "lucid_age lucid_female lucid_hhi lucid_incmissing i.lucid_education partisanship_*"
		local sn = "07_notnortheast"
		drop if lucid_region==3
		}
	if `ctr'==8 {
		local subgroup "Democrats"
		local covariates = "lucid_age lucid_female lucid_hhi lucid_incmissing i.lucid_education i.lucid_region"
		local sn = "08_dems"
		keep if lucid_party7==1 | lucid_party7==2 | lucid_party7==3
		}
 	if `ctr'==9 {
		local subgroup "Republicans"
		local covariates = "lucid_age lucid_female lucid_hhi lucid_incmissing i.lucid_education i.lucid_region"
		local sn = "09_reps"
		keep if lucid_party7==5 | lucid_party7==6 | lucid_party7==7
		}
 	if `ctr'==10 {
		local subgroup "Low Liberty Scale"
		local covariates = "lucid_age lucid_female lucid_hhi lucid_incmissing i.lucid_education partisanship_* i.lucid_region"
		local sn = "10_lowliberty"
		keep if liberty_scale<.65
		}		
 	if `ctr'==11 {
		local subgroup "High Liberty Scale"
		local covariates = "lucid_age lucid_female lucid_hhi lucid_incmissing i.lucid_education partisanship_* i.lucid_region"
		local sn = "11_highliberty"
		keep if liberty_scale>=.65
		}		
	
	di "Subgroup `subgroup'"
	
	foreach var of varlist normsbeliefs_scale sd_scale food_scale others_scale  {
			regress `var' treat_* `covariates', robust
			est store `var'

			qui sum `var' if e(sample)

			local templabel : var label `var'
		}

	coefplot normsbeliefs_scale sd_scale food_scale others_scale, keep(*:treat*) omitted baselevels xline(0) yline(6.5 8.5) title("Effects by Message and Outcome", size(small)) xlabel(, labels labsize(vsmall)) ylabel(, labels labsize(small)) legend(order(2 "BELIEFS" 4 "DISTANCING" 6 "FOOD" 8 "OTHERS") size(vsmall) on) title("`subgroup'") name(sg`sn', replace) 
	
	restore
}

*Create the figures for the S3 Appendix 
*graph combine sg02_young sg03_old, title("Figure A2: Experiment 1, Results by Age") xsize(8.5) ysize(11) scale(.8) name(hetbyage, replace) 
graph combine sg02_young sg03_old, xsize(8.5) ysize(11) scale(.8) name(hetbyage, replace) 
graph export graphs\FigureA2.pdf, replace

*graph combine sg04_men sg05_women, title("Figure A3: Experiment 1, Results by Gender") xsize(8.5) ysize(11) scale(.8) name(hetbygender, replace) 
graph combine sg04_men sg05_women, xsize(8.5) ysize(11) scale(.8) name(hetbygender, replace) 
graph export graphs\FigureA3.pdf, replace

*graph combine sg08_dems sg09_reps, title("Figure A4: Experiment 1, Results by Partisanship") xsize(8.5) ysize(11) scale(.8) name(hetbypid, replace) 
graph combine sg08_dems sg09_reps, xsize(8.5) ysize(11) scale(.8) name(hetbypid, replace) 
graph export graphs\FigureA4.pdf, replace

*graph combine sg06_northeast sg07_notnortheast, title("Figure A5: Experiment 1, Results by Location") xsize(8.5) ysize(11) scale(.8) name(hetbylocation, replace) 
graph combine sg06_northeast sg07_notnortheast, xsize(8.5) ysize(11) scale(.8) name(hetbylocation, replace) 
graph export graphs\FigureA5.pdf, replace

*graph combine sg10_lowliberty sg11_highliberty, title("Figure A6: Experiment 1, Results by Liberty Endorsement") xsize(8.5) ysize(11) scale(.8) name(hetbylib, replace) 
graph combine sg10_lowliberty sg11_highliberty, xsize(8.5) ysize(11) scale(.8) name(hetbylib, replace) 
graph export graphs\FigureA6.pdf, replace


*********EXPERIMENT 2*********
use "Experiment2.dta", clear

**Generate Outcome Scales for Experiment 2 and examine reliability
*Social Distancing
* reverse code measures in Social distancing behavior scale
	foreach var of varlist ly_church ly_library ly_electivemed ly_familyhouse ly_friendshouse_inside ly_protest ly_publictransport ly_airplane ly_workoutsidehome meetings_smalloutside meetings_smallinside meetings_bigoutside meetings_biginside {
	 gen prc_`var' = abs(1-`var')
	local templabel : var label `var'
	label var prc_`var' "RC `templabel'"

	 }
	unab temp: prc_*

	egen outcome_sdscale_ct=rownonmiss(agree_liketosocdist ly_selfisolate authorities_alertifpositive `temp')
	* Perform mean imputation for scale items
	foreach var of varlist agree_liketosocdist ly_selfisolate authorities_alertifpositive `temp' {
		gen Z`var' = `var'
		qui summ `var'
		replace Z`var'=`r(mean)' if Z`var'==.
		}
	egen outcome_sdscale=rowtotal(Z*)
	replace outcome_sdscale=outcome_sdscale/16
	* And make outcome missing if more than 25% of scale items are missing
	replace outcome_sdscale=. if outcome_sdscale_ct<12
	*label var outcome_sdscale "Social distancing scale (16 items, 0-1)"
	label var outcome_sdscale "DISTANCING"
	*examine reliability
	alpha prc_* agree_liketosocdist ly_selfisolate authorities_alertifpositive
	
	
*Figure 3 (done here so prc_ prefix can be used later
local spec = "individualitems"
local specdesc = "Individual items."
foreach var of varlist agree_liketosocdist ly_selfisolate authorities_alertifpositive auth_contacttrace prc_ly_church prc_ly_familyhouse_inside prc_ly_friendshouse_inside  {
		regress `var' i.D_* treatment_baseline treatment_linear treatment_notbravery, robust
		est store `var'
	
		lincom treatment_linear-treatment_baseline
		local t_l_v_b_e=`r(estimate)'
		lincom treatment_notbravery-treatment_baseline
		local t_n_v_b_e=`r(estimate)'
		lincom ((treatment_linear+treatment_notbravery)/2)-treatment_baseline
		local t_p_v_b_e=`r(estimate)'
	}
	
coefplot agree_liketosocdist ly_selfisolate authorities_alertifpositive auth_contacttrace prc_ly_church prc_ly_familyhouse_inside prc_ly_friendshouse_inside, keep(*:treatment*) omitted baselevels xline(0)  title("Effects by Message and Outcome", size(small)) xlabel(, labels labsize(vsmall)) ylabel(, labels labsize(small))legend(order(2 "SD"  4 "Self-Isolate"  6 "Alert authorities"  8 "Cooperate Contact Trace"  10 "(Not) Go to religious service"  12 "(Not) Go to family's house inside"  14 "(Not) Go to friend's house inside" ) size(vsmall) region(lstyle(none)) on)  name(itembyitem, replace) 
graph export graphs\Figure3.pdf, replace

	
	*PRC prefix is reused in construction of subsequent scales, so drop those items here now that the scale is construted.
	drop Z* prc_* 
	
 *Food behavior scale

	* reverse code measures in food behavior scale
	foreach var of varlist ly_coffeeshop ly_outtoeat_inside ly_outtoeat_outside ly_bar {
	 gen prc_`var' = abs(1-`var')
	 }
	unab temp: prc_*

	egen outcome_foodscale_ct=rownonmiss(`temp')
	* Perform mean imputation for scale items
	foreach var of varlist `temp' {
		gen Z`var' = `var'
		qui summ `var'
		replace Z`var'=`r(mean)' if Z`var'==.
		}
	egen outcome_foodscale=rowtotal(Z*)
	replace outcome_foodscale=outcome_foodscale/4
	* And make outcome missing if more than 25% of scale items are missing
	replace outcome_foodscale=. if outcome_sdscale_ct<3
	*label var outcome_foodscale "Food behavior scale (4 items, 0-1)"
	label var outcome_foodscale "FOOD"
	alpha prc_* 
	
	*PRC prefix is reused in construction of subsequent scales, so drop those items here now that the scale is construted.
	drop Z* prc_* 

	
* Others

	* reverse code measures in peer evaluations scale
	foreach var of varlist noncompliers_trustworthy noncompliers_likeable noncompliers_competent noncompliers_intelligent {
	 gen prc_`var' = abs(1-`var')
	 }
	unab temp: prc_*

	egen outcome_peers_ct=rownonmiss(persuade_likely report_likely noncompliers_selfish auth_contacttrace `temp')
	* Perform mean imputation for scale items
	foreach var of varlist persuade_likely report_likely noncompliers_selfish auth_contacttrace `temp' {
		gen Z`var' = `var'
		qui summ `var'
		replace Z`var'=`r(mean)' if Z`var'==.
		}
	egen outcome_peersscale=rowtotal( Z*)
	replace outcome_peersscale=outcome_peersscale/8
	* And make outcome missing if more than 25% of scale items are missing
	replace outcome_peersscale=. if outcome_peers_ct<6
	label var outcome_peersscale "Peer behavior scale (8 items, 0-1)"
	label var outcome_peersscale "OTHERS"
	factor prc_* persuade_likely report_likely noncompliers_selfish auth_contacttrace 
	alpha prc_* persuade_likely report_likely noncompliers_selfish auth_contacttrace 
	drop Z* prc_*
	
* Beliefs
	egen outcome_beliefsscale=rowtotal( agree_socdiststopmesick agree_socdiststopotherssick agree_guiltyifnotsocdist)
	replace outcome_beliefsscale=outcome_beliefsscale/3
	replace outcome_beliefsscale=. if agree_socdiststopmesick==. | agree_socdiststopotherssick==. |  agree_guiltyifnotsocdist==.
	*label var outcome_beliefsscale "Beliefs scale (3 items, 0-1)"
	label var outcome_beliefsscale "BELIEFS"
	alpha agree_socdiststopmesick agree_socdiststopotherssick agree_guiltyifnotsocdist

* Masking behavior

	gen maskdiff=((ly_store_reqmasks - ly_store_banmasks)+1)/2
	egen outcome_masks_ct=rownonmiss(mask_work mask_pubtransport mask_walk mask_shopinside mask_visitfriend mask_park maskdiff)

	* Perform mean imputation for scale items
	foreach var of varlist mask_work mask_pubtransport mask_walk mask_shopinside mask_visitfriend mask_park maskdiff {
		gen Z`var' = `var'
		qui summ `var'
		replace Z`var'=`r(mean)' if Z`var'==.
		}
	egen outcome_maskscale=rowtotal( Z*)
	replace outcome_maskscale=outcome_maskscale/7
	* And make outcome missing if more than 25% of scale items are missing
	replace outcome_maskscale=. if outcome_masks_ct<7
	*label var outcome_maskscale "Masking behavior scale (7 items, 0-1)"
	label var outcome_maskscale "MASKS"
	alpha mask_work mask_pubtransport mask_walk mask_shopinside mask_visitfriend mask_park maskdiff
	drop Z*
	
	
*Estimate models for Experiment 2
local spec = "main"
local specdesc = "Main specification."
foreach var of varlist outcome_beliefsscale outcome_sdscale outcome_foodscale outcome_peersscale outcome_maskscale {
		regress `var' i.D_* treatment_baseline treatment_linear treatment_notbravery, robust
		est store `var'
	
		* Save baseline estimate for graphing
		local b_`var'=_b[treatment_baseline]
	}


*Figure 2
*Make left 3 panels
coefplot outcome_beliefsscale, levels(90) keep(*:treatment*) omitted baselevels xline(0) xline(`b_outcome_beliefsscale',  lpattern(dash)) title("A: BELIEFS", size(small)) xlabel(, labels labsize(vsmall)) ylabel(, labels labsize(small)) name(F2a, replace) fxsize(60)
coefplot outcome_foodscale, levels(90) keep(*:treatment*) omitted baselevels xline(0) xline(`b_outcome_foodscale',  lpattern(dash)) title("C: FOOD", size(small)) xlabel(, labels labsize(vsmall)) ylabel(, labels labsize(small)) name(F2c, replace) fxsize(60)
coefplot outcome_maskscale, levels(90) keep(*:treatment*) omitted baselevels xline(0) xline(`b_outcome_maskscale',  lpattern(dash)) title("E: MASKS", size(small)) xlabel(, labels labsize(vsmall)) ylabel(, labels labsize(small)) name(F2e, replace) fxsize(60)

*temporarily replace label names for right panels
preserve

foreach var of varlist treatment* {
	label var `var' " "
}

coefplot outcome_sdscale, levels(90) keep(*:treatment*) omitted baselevels xline(0) xline(`b_outcome_sdscale',  lpattern(dash)) title("B: DISTANCING", size(small)) xlabel(, labels labsize(vsmall)) ylabel(, labels labsize(small)) name(F2b, replace) fxsize(40)
coefplot outcome_peersscale, levels(90) keep(*:treatment*) omitted baselevels xline(0) xline(`b_outcome_peersscale',  lpattern(dash)) title("D: OTHERS", size(small)) xlabel(, labels labsize(vsmall)) ylabel(, labels labsize(small)) name(F2d, replace) fxsize(40)
restore

*Merge all panels and export figure
graph combine F2a F2b F2c F2d F2e, rows(3) title("") note("", size(vsmall)) xsize(8.5) ysize(11) scale(.7) xcommon
graph export graphs\Figure2.pdf, replace
	

*Distribution of Outcomes Scale Outcomes S5 Appendix
foreach var of varlist outcome_beliefsscale outcome_sdscale outcome_foodscale outcome_peersscale outcome_maskscale  {
	summ `var'
	local t1=strofreal(r(mean),"%9.2f")
	local t2=strofreal(r(sd),"%9.2f")
	twoway (histogram `var', fraction bin(10)), name(`var', replace) note("Mean = `t1'; SD=`t2'")
}

graph combine outcome_beliefsscale outcome_sdscale outcome_foodscale outcome_peersscale outcome_maskscale , xsize(8.5) ysize(11) scale(.7)
graph export graphs\FigureA7DistOfOutcomes.pdf, replace



*Liberty Endorsement Analysis S6 Appendix

*Create liberty scale 
gen liberty_scale = (liberty1+ liberty2+liberty3+abs(liberty4-1)+liberty5)/5
label var liberty_scale "Liberty endorsement (Scale 0-1)"

*Create binary liberty endorsement
summ liberty_scale
gen H1_highlib=liberty_scale>`r(mean)' & liberty_scale~=.
label var H1_highlib "High liberty"

*Next 3 aren't reported in manuscript, but still interesting. 
gen H2_republican=0 if pid7_with_leaners=="Democrat"
replace H2_republican=1 if pid7_with_leaners=="Republican"
label var H2_republican "Republican"

gen H3_woman=gender=="Female"
label var H3_woman "Female"

gen H4_chooseworkaway=.
replace H4_chooseworkaway=0 if employsets=="Have to work outside home" |  employsets=="Working just at home"
replace H4_chooseworkaway=1 if employsets=="Choose to to work outside home"
label var H4_chooseworkaway "Choose work outside home"

*Create the dummies for HTE
forvalues ctr = 1(1)4 {
	foreach var of varlist treatment_baseline treatment_linear treatment_notbravery {
		gen ZH`ctr'_`var' = H`ctr'_ * `var'
		gen ZHnot`ctr'_`var' = (1-H`ctr'_) * `var'
		local templabel : var label `var'
		local templabel = subinstr("`templabel'"," treatment","",.)
		di "`templabel'"
		unab temp: H`ctr'_
		di "`temp'"
		*local templabel : var label (variablename)
		local hetlabel : var label `temp'
		di "`hetlabel'"
		label var ZH`ctr'_`var' "`hetlabel' X `templabel'"
		label var  ZHnot`ctr'_`var' "NOT `hetlabel' X `templabel'"
		
		}
	}

local spec = "het_lib"
local specdesc = "By Liberty Endorsement."
*rename D_newpid7 Q_newpid7
foreach var of varlist outcome_beliefsscale outcome_sdscale outcome_foodscale outcome_peersscale outcome_maskscale {
	regress `var' i.D_* H1_highlib ZH*1_* , robust
	est store `var'
}

*create figure for liberty endorsement results
coefplot outcome_beliefsscale outcome_sdscale outcome_foodscale outcome_peersscale  outcome_maskscale, keep(*:ZH*1_**) omitted baselevels levels(90) xline(0) title("Primary Outcomes", size(small)) xlabel(, labels labsize(vsmall)) ylabel(, labels labsize(small)) legend(order(2 "BELIEFS" 4 "OWN-SD" 6 "FOOD" 8 "OTHERS" 10 "MASKS") size(vsmall) on) name(primary, replace) 
graph export graphs\LibertyHTE.pdf, replace
